<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../../admin/css/pearCommon.css" />
    <link rel="stylesheet" href="../../../component/layui/css/layui.css" />
</head>
<body>
<form class="layui-form" action="" lay-filter="edit-form">
    <input type="hidden" name="sid" id="sid">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input id="ssname" type="text" name="sname" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">卡号</label>
                    <div class="layui-input-block">
                        <input type="text"  name="cardId" lay-verify="title" autocomplete="off" placeholder="请输入卡号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">生日</label>
                    <div class="layui-input-block">
                        <input type="text"  name="birthday" id="birthday" class="layui-input" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block">
                        <input type="text"  name="address" lay-verify="title" autocomplete="off" placeholder="请输入地址" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="男" title="男">
                        <input type="radio" name="gender" value="女" title="女" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">班级</label>
                    <div class="layui-input-block">
                        <select name="clazz" lay-verify="required" id="select_clazz">
                            <option value=""></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="student-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script>

layui.use(['form','jquery','laydate'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    var laydate = layui.laydate;

    let API_CONTEXT_PATH = "http://localhost:8080/api"
    //console.log(parent.editRowData)
    //parent代表父窗口(window)
    let row = JSON.parse(parent.editRowData);
    //填充form
    form.val("edit-form",row);
    //给保存ID的隐藏域赋值
   // $("#sid").val(row.sid);
    //渲染班级下拉框
    $.get(API_CONTEXT_PATH+"/clazzes",function (clazzes) {
        let selectBirthday = $("#select_clazz");
        for(c of clazzes){
            //把修改的这条学生的班级 默认选中
            if(row.clazz && row.clazz.cid == c.cid){
                selectBirthday.append("<option value='"+c.cid+"' selected='selected'>"+c.cname+"</option>");
            }else{
                selectBirthday.append("<option value='"+c.cid+"'>"+c.cname+"</option>");
            }
        }
        //重新渲染form
        form.render();
    })


    //执行一个laydate实例
    laydate.render({
        elem: '#birthday' //指定元素
    });



    form.on('submit(student-save)', function(data){
        $.ajax({
            url:API_CONTEXT_PATH+'/student',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType:'application/json',
            type:'put',
            success:function(result){
                if(result.msg="success"){
                    layer.msg(result.msg,{icon:1,time:1000},function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("studentTable");
                    });
                }else{
                    layer.msg(result.msg,{icon:2,time:1000});
                }
            }
        })
        return false;
    });
})
</script>
<script>
</script>
</body>
</html>